package com.product.controller;

import com.product.enmu.HttpStatusEnum;
import com.product.handler.ParameterValidationException;
import com.product.result.ApiResult;
import com.product.service.IOrdersService;
import com.product.to.AddOrderTo;
import com.product.to.UpdateOrderTo;
import com.product.utils.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "订单控制器")
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private IOrdersService orderService;

    @Parameters({
            @Parameter(name = "size", description = "每页条数，默认10条"),
            @Parameter(name = "current", description = "分页页数，默认第一页"),
            @Parameter(name = "orderState", description = "订单状态"),
            @Parameter(name = "customerName", description = "客户名字"),
            @Parameter(name = "orderId", description = "订单id"),
            @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    })
    @Operation(summary = "获取所有订单")
    @GetMapping("/all")
    private ApiResult<PageUtils> getAllOrders(@RequestParam(name = "size", defaultValue = "10") Integer size,
                                              @RequestParam(name = "current", defaultValue = "1") Integer current,
                                              @RequestParam(name = "orderState", required = false) Integer orderState,
                                              @RequestParam(name = "customerName", required = false) String customerName,
                                              @RequestParam(name = "orderId", required = false) Long orderId,
                                              @RequestParam(name = "timeOrderBy", defaultValue = "1") Integer timeOrderBy
    ) {
        return ApiResult.ok(orderService.getAllOrder(size, current, orderState, customerName, orderId,timeOrderBy));
    }

    @Parameters({
            @Parameter(name = "size", description = "每页条数，默认10条"),
            @Parameter(name = "current", description = "分页页数，默认第一页"),
            @Parameter(name = "orderState", description = "订单状态"),
            @Parameter(name = "customerName", description = "客户名字"),
            @Parameter(name = "orderId", description = "订单id"),
            @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    })
    @Operation(summary = "获取所有订单和订单项")
    @GetMapping("/allOrder")
    private ApiResult<PageUtils> getAllOrderByItem(@RequestParam(name = "size", defaultValue = "10") Integer size,
                                                   @RequestParam(name = "current", defaultValue = "1") Integer current,
                                                   @RequestParam(name = "orderState", required = false) Integer orderState,
                                                   @RequestParam(name = "customerName", required = false) String customerName,
                                                   @RequestParam(name = "orderId", required = false) Long orderId,
                                                   @RequestParam(name = "timeOrderBy", defaultValue = "1")Integer timeOrderBy) {
        return ApiResult.ok(orderService.getAllOrderByItem(size, current, orderState, customerName, orderId,timeOrderBy));
    }

    @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    @Operation(summary = "添加订单")
    @PostMapping("/add")
    private ApiResult<Object> addOrder(@Valid @RequestBody AddOrderTo order, BindingResult result) throws ParameterValidationException {
        if (result.hasErrors()) {
            StringBuilder errors = new StringBuilder();
            result.getAllErrors().forEach(error -> errors.append(error.getDefaultMessage()).append("\n"));
            throw new ParameterValidationException(errors.toString(), HttpStatusEnum.PARAM_ERROR.getCode());
        }
        orderService.addOrder(order);
        return ApiResult.ok();
    }

    @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    @Operation(summary = "修改订单")
    @PostMapping("/update")
    private ApiResult<Object> updateOrder(@Valid @RequestBody UpdateOrderTo order, BindingResult result) throws ParameterValidationException {
        if (result.hasErrors()) {
            StringBuilder errors = new StringBuilder();
            result.getAllErrors().forEach(error -> errors.append(error.getDefaultMessage()).append("\n"));
            throw new ParameterValidationException(errors.toString(), HttpStatusEnum.PARAM_ERROR.getCode());
        }
        orderService.updateOrder(order);
        return ApiResult.ok();
    }

    @Parameters({
            @Parameter(name = "orderId", description = "订单id"),
            @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    })
    @Operation(summary = "删除订单")
    @PostMapping("/delete")
    private ApiResult<Object> deleteOrder(@RequestParam(name = "orderId") Long orderId) {
        orderService.deleteOrder(orderId);
        return ApiResult.ok();
    }

    @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    @Operation(summary = "批量删除订单")
    @PostMapping("/deletes")
    private ApiResult<Object> deleteOrders(@RequestBody List<Long> orderIds) {
        orderService.deleteOrders(orderIds);
        return ApiResult.ok();
    }
}
